<template>
    <a-modal v-model:open="visible" title="选择用户" :destroy-on-close="destroyOnClose" width="1000px" :mask-closable="maskClosable" @ok="submit" @cancel="close">
        <Index ref="indexRef" />
    </a-modal>
</template>
<script setup>
import { ref, nextTick } from 'vue'
import { notice } from '@iframe/tools/components'
import { useModal } from '@iframe/tools/mixins/ModelDrawerMixins'
import Index from './index.vue'

const { visible, destroyOnClose, maskClosable, close } = useModal()
const indexRef = ref()
const emits = defineEmits(['close'])

const open = selects => {
    const ids = selects.map(item => item.id)
    visible.value = true
    nextTick(() => {
        indexRef.value.setSelectKeys(ids)
    })
}
const submit = () => {
    const list = indexRef.value.getSelectList()
    if (!list || list.length === 0) {
        notice.error('请选择用户')
        return
    }
    const users = list.map(item => {
        return { id: item.id, name: item.nicename }
    })
    emits('close', users)
    close()
}
defineExpose({ open })
</script>
